/* (c)2010-2011 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.web;

import java.util.SortedSet;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

import org.ajm.spring.core.model.AbstractPersistentEntity;
import org.ajm.spring.survey.modules.admin.model.Answer;
import org.ajm.spring.survey.modules.admin.model.AnswerPositionComparator;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

/**
 * The command object used to bind the values in the form to change the order of
 * answers.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
public class ChangeAnswerOrderCommand extends AbstractPersistentEntity implements Comparable<ChangeAnswerOrderCommand> {
    
    // ------------------------------------------------------------------------|
    // --- CONSTANTS ----------------------------------------------------------|
    // ------------------------------------------------------------------------|

    private static final long serialVersionUID = 2425297111167281474L;

    // ------------------------------------------------------------------------|
    // --- CLASS VARIABLES ----------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- INSTANCE VARIABLES -------------------------------------------------|
    // ------------------------------------------------------------------------|

    private SortedSet<Answer> answers = new TreeSet<Answer>(new AnswerPositionComparator());

    // ------------------------------------------------------------------------|
    // --- CONSTRUCTORS -------------------------------------------------------|
    // ------------------------------------------------------------------------|

    /**
     * Default empty constructor.
     */
    public ChangeAnswerOrderCommand() {
        
        super();
    }

    // ------------------------------------------------------------------------|

    /**
     * @param answers 
     */
    public ChangeAnswerOrderCommand(SortedSet<Answer> answers) {
        
        super();
        if (answers != null) this.answers = answers;
    }

    // ------------------------------------------------------------------------|
    // --- CLASS METHODS ------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- INSTANCE METHODS ---------------------------------------------------|
    // ------------------------------------------------------------------------|
    
    /**
     * Adds the ability to perform order comparisons.
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(ChangeAnswerOrderCommand that) {
        
        return new CompareToBuilder()

        .append(this.getDateCreated(), that.getDateCreated())
        
        .append(this.getAnswers(), that.getAnswers())
        
        .toComparison();

    }
    
    // ------------------------------------------------------------------------|
    
    /**
     * Implements the concept of logical equality for this object.
     *
     * @see java.lang.Object#hashCode()
     * @see java.lang.Object#equals(Object)
     */
    @Override
    public boolean equals(Object object) {

        if (this == object)
            return true;

        if (!(object instanceof ChangeAnswerOrderCommand)) {
            return false;
        }

        final ChangeAnswerOrderCommand that = (ChangeAnswerOrderCommand) object;

        return new EqualsBuilder()
        
        .append(this.getAnswers(), that.getAnswers())

        .isEquals();
    }

    // ------------------------------------------------------------------------|

    /**
     * Compute the hash code for this object. This is necessary when overriding
     * equals since equal objects must have equal hash codes.
     * 
     * @see java.lang.Object#hashCode()
     * @see java.lang.Object#equals(Object)
     */
    @Override
    public int hashCode() {

        return new HashCodeBuilder(-83574467, 150340475)

        .append(this.getAnswers())

        .toHashCode();
    }

    // ------------------------------------------------------------------------|

    /**
     * Obtain a string representation of this object.
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {

        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)

        .append("answers ", this.getAnswers())

        .toString();
    }

    // ------------------------------------------------------------------------|
    
    /**
     * @return the answers
     */
    @OneToMany(mappedBy = "question", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
    @Sort(type = SortType.COMPARATOR, comparator = AnswerPositionComparator.class)
    public SortedSet<Answer> getAnswers() {
        return this.answers;
    }

    // ------------------------------------------------------------------------|
    
    /**
     * @param answers the answers to set
     */
    public void setAnswers(SortedSet<Answer> answers) {
        this.answers = answers;
    }

    // ------------------------------------------------------------------------|
    
}
